package com.haonan.springboot_exercise.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.haonan.springboot_exercise.bean.User;
import com.haonan.springboot_exercise.bean.pojo.JwtToken;
import com.haonan.springboot_exercise.bean.pojo.Result;
import com.haonan.springboot_exercise.bean.pojo.JwtUtil;
import com.haonan.springboot_exercise.bean.pojo.UserInfo;
import com.haonan.springboot_exercise.dao.UserDao;
import com.haonan.springboot_exercise.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author haonan
 * @date 2021/4/14 23:01
 **/
@Component
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserDao userDao;

    @Override
    public Result login(User user) throws Exception {
        if(user.getUser_name()==""||user.getUser_password()==""){
            return Result.fail("请输入完整的用户名和密码");
        }
        User u = userDao.getUserByMessage(user.getUser_name(), user.getUser_password());//从数据库查询该用户
        if(u==null){
            return Result.fail("用户名或密码错误");
        }else if(!u.getState_flag()){
            return Result.disable("该用户已被禁用");
        }else{
            //登入成功，根据用户名生成一个token，并且token
            JwtToken jwtToken=new JwtToken();
            jwtToken.setToken(JwtUtil.createToken(user.getUser_name(),user.getUser_password()));
            return Result.succ(jwtToken);
        }
    }

    @Override
    public Result info(String token) throws Exception {
        Result res=new Result();
        //解析token
        Map<String, Claim> map = JwtUtil.verifyToken(token);

        if(map!=null){      //token有效
            String user_name=map.get("user_name").asString();//用户名
            String user_password=map.get("user_password").asString();//密码
            User u = userDao.getUserByMessage(user_name, user_password);//从数据库查询该用户
            System.out.println("从token中解析出了user");
            System.out.println(u);

            if(u.getState_flag()==true){ //可以使用
                UserInfo info=new UserInfo();//新建一个返回给vue的对象
                //设置用户头像图片，之后可以用mongodb存储
                info.setAvatar("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F9fo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fwh%253D450%252C600%2Fsign%3D0b696fecdd1373f0f56a679b913f67cd%2Fd009b3de9c82d1581695ae268c0a19d8bc3e4216.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622726747&t=8769df551f30985ac716b629ec911f8e");
                info.setUser_name(u.getUser_name());//用户名
                info.setIntroduction("没啥好说的");
                List<String> roles=null;
                if(u.getRole_id()){
                    roles= Arrays.asList("admin");//系统用户
                }else{
                    roles= Arrays.asList("user");//普通用户
                }
                info.setRoles(roles);
                info.setState_flag(u.getState_flag());
                info.setPop_flag(u.getPop_flag());
                info.setSmtp_flag(u.getSmtp_flag());
                res=Result.succ("正确登陆",info);
            }else{//被禁用
                res=Result.fail("该用户被禁用");
            }
        }else{  //不存在有效的token
            res=Result.fail("token无效");
        }
        return res;
    }

    @Override
    public Result logout() throws Exception {
        return Result.succ("成功退出");
    }
}
